package com.toocms.jinjuhang.ui.mine.setting.agreement

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.data.system.AgreementData
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.bus.event.SingleLiveEvent

class AgreementModel(application: Application, bundle: Bundle?) :
    BaseViewModel<BaseModel>(application) {
    val type by lazy {
        bundle?.getString(Constants.BundleKey.KEY_TYPE) ?: Constants.Extra.AGREEMENT_TYPE_USER
    }

    val titleSingleLiveEvent = SingleLiveEvent<String>()
    val contentObservableField = ObservableField<String>()

    init {
        titleSingleLiveEvent.value = when (type) {
            Constants.Extra.AGREEMENT_TYPE_USER -> StringUtils.getString(R.string.str_user_agreement)
            Constants.Extra.AGREEMENT_TYPE_PRIVACY -> StringUtils.getString(R.string.str_privacy_agreement)
            else -> StringUtils.getString(R.string.str_user_agreement)
        }
        val url = when (type) {
            Constants.Extra.AGREEMENT_TYPE_USER -> Urls.System.USER_AGREEMENT
            Constants.Extra.AGREEMENT_TYPE_PRIVACY -> Urls.System.PRIVACY_AGREEMENT
            else -> Urls.System.USER_AGREEMENT
        }
        requestData(url, resultClass = AgreementData::class.java, result = {
            contentObservableField.set(it.content)
        })
    }
}